#include <stdint.h>
#include <threads.h>

/* 4 bytes IP address */
typedef struct ip_address {
	uint8_t byte1;
	uint8_t byte2;
	uint8_t byte3;
	uint8_t byte4;
}ip_address;

/* IPv4 header */
typedef struct ip_header {
	uint8_t	ver_ihl;		// Version (4 bits) + Internet header length (4 bits)
	uint8_t	tos;			// Type of service 
	uint16_t tlen;			// Total length 
	uint16_t identification; // Identification
	uint16_t flags_fo;		// Flags (3 bits) + Fragment offset (13 bits)
	uint8_t	ttl;			// Time to live
	uint8_t	proto;			// Protocol
	uint16_t crc;			// Header checksum
	ip_address	saddr;		// Source address
	ip_address	daddr;		// Destination address
	uint32_t	op_pad;			// Option + Padding
}ip_header;

/* UDP header*/
typedef struct udp_header {
	uint16_t sport;			// Source port
	uint16_t dport;			// Destination port
	uint16_t len;			// Datagram length
	uint16_t crc;			// Checksum
}udp_header;

typedef struct dns_header {
	uint16_t	T_ID;
	uint16_t	flags;
	uint16_t	Questions;
	uint16_t Answer_RRs;
	uint16_t Authority_RRs;
	uint16_t Additional_RRs;
}dns_header;

typedef struct dns_answer {
	uint16_t name;
	uint16_t type;
	uint16_t class_type;
	uint16_t ttl1;
	uint16_t ttl2;
	uint16_t datalen;
	ip_address addr;
}dns_answer;

/* prototype of the packet handler */
void packet_handler(uint8_t* param, const struct pcap_pkthdr* header, const uint8_t* pkt_data);

int start(cnd_t* cnd, int interactive, uint16_t src_port);